package com.leetcode.partition14;

import com.leetcode.common.TreeNode;

/**
 * @author rkc
 * @date 2022/9/8 9:33
 */
public class LC1372二叉树中的最长交错路径 {

    private int ans = 0;

    public int longestZigZag(TreeNode root) {
        dfs(root, -1);
        return ans;
    }

    private int dfs(TreeNode u, int d) {
        if (u == null) return 0;
        int l = dfs(u.left, 0), r = dfs(u.right, 1);
        ans = Math.max(ans, Math.max(l, r));
        if (d == 0) return r + 1;
        return l + 1;
    }
}
